#include "uart.h"
#include "Tasks.h"
#define TX_LEN 512
#define RX_LEN 512
unsigned char Tx_Buf_Uart3[TX_LEN];
unsigned char Rx_Buf_Uart3[RX_LEN];
unsigned char Tx_Buf_Uart1[TX_LEN];
unsigned char Rx_Buf_Uart1[RX_LEN];
unsigned char Tx_Buf_Uart5[TX_LEN];
unsigned char Rx_Buf_Uart5[RX_LEN];
int Flag_Tx_Uart3_Busy=0;
int Flag_Tx_Uart1_Busy=0;
int Flag_Tx_Uart5_Busy=0;

#define UART1_TX_PIN 	GPIO_Pin_9
#define UART1_RX_PIN	GPIO_Pin_10
#define UART3_TX_PIN	GPIO_Pin_10
#define UART3_RX_PIN	GPIO_Pin_11
#define UART5_TX_PIN	GPIO_Pin_12
#define UART5_RX_PIN	GPIO_Pin_2



/* --------------------- 串口初始化代码 --------------------------*/


/**
 * @Description 串口5初始化 用于与超声波模块通信
 */
void Uart5_to_Ultra_Init(u32 Bound)
{	
	//GPIO端口设置
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	DMA_InitTypeDef DMA_InitStructure;  
  
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); 	//使能GPIOC时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE); 	//使能GPIOC时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE); 	//使能UART5时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); 	//使能DMA1时钟
	
	//串口5对应引脚复用映射
	GPIO_PinAFConfig(GPIOC,UART5_TX_PIN,GPIO_AF_UART5);  //GPIOC12复用为UART5
	GPIO_PinAFConfig(GPIOD,UART5_RX_PIN,GPIO_AF_UART5); 	 //GPIOD2复用为UART5
	
	//串口发送DMA中断设置
	NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream7_IRQn;  
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4;  
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
	NVIC_Init(&NVIC_InitStructure);  
	
	
	//串口发送DMA通道配置
	DMA_DeInit(DMA1_Stream7);  
	DMA_InitStructure.DMA_Channel = DMA_Channel_4;   
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&UART5->DR);  
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Tx_Buf_Uart5;  
	DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;  
	DMA_InitStructure.DMA_BufferSize = TX_LEN;   
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;  
	DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;   
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;  
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;  
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;       
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;          
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;          
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;   
           
	DMA_Init(DMA1_Stream7, &DMA_InitStructure);    
	//使能 串口发送DMA 传输完成中断
	DMA_ITConfig(DMA1_Stream7,DMA_IT_TC,ENABLE);     
	
	
	//串口接收DMA通道配置  
	DMA_DeInit(DMA1_Stream0);  
	DMA_InitStructure.DMA_Channel = DMA_Channel_4;   
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&UART5->DR);  
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Rx_Buf_Uart5;   
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;   
	DMA_InitStructure.DMA_BufferSize = RX_LEN;   
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;   
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;   
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;  
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;    
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;    
	DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;  
  
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;       
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;           
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;           
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;   
		      
	DMA_Init(DMA1_Stream0, &DMA_InitStructure);    
	//使能通道
	DMA_Cmd(DMA1_Stream0,ENABLE); 


	//UART5端口配置
	GPIO_InitStructure.GPIO_Pin = UART5_TX_PIN; 			//GPIOC12
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;			//复用功能
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		//速度50MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 			//推挽复用输出
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 			//上拉
	GPIO_Init(GPIOC,&GPIO_InitStructure); 
	
	GPIO_InitStructure.GPIO_Pin = UART5_RX_PIN; 				//GPIOD2
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;			//复用功能
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		//速度50MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 			//推挽复用输出
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 			//上拉
	GPIO_Init(GPIOD,&GPIO_InitStructure);
	
	//UART5 初始化设置
	USART_InitStructure.USART_BaudRate = Bound;											//波特率设置
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;							//字长为8位数据格式
	USART_InitStructure.USART_StopBits = USART_StopBits_1;								//一个停止位
	USART_InitStructure.USART_Parity = USART_Parity_No;									//无奇偶校验位
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;		//无硬件数据流控制
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;						//收发模式
	USART_Init(UART5, &USART_InitStructure); 											//初始化串口5
	
	/*
	USART_ITConfig(UART5,USART_IT_TC,DISABLE);  
	USART_ITConfig(UART5,USART_IT_RXNE,DISABLE);  
	USART_ITConfig(UART5,USART_IT_IDLE,ENABLE); 
	*/	

	//Uart5 NVIC 配置
	NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;				//串口5中断通道
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=4;			//抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =2;				//子优先级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;					//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);									//根据指定的参数初始化VIC寄存器
	
	//使能串口DMA收发 
	USART_DMACmd(UART5,USART_DMAReq_Tx,ENABLE);
	USART_DMACmd(UART5,USART_DMAReq_Rx,ENABLE);
	
	//使能串口中断
	USART_ITConfig(UART5,USART_IT_TC,DISABLE);  
	USART_ITConfig(UART5,USART_IT_RXNE,DISABLE);  
	USART_ITConfig(UART5,USART_IT_TXE,DISABLE);  
	USART_ITConfig(UART5,USART_IT_IDLE,ENABLE);  
	
	USART_Cmd(UART5, ENABLE);  										
}


/**
 * @Description 串口1初始化、用于与电脑通信
 */
void Uart1_to_PC_Init(u32 Bound)
{
	//GPIO端口设置
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;	
	DMA_InitTypeDef DMA_InitStructure;   
  
	RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);  
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);  
  
	//DMA发送中断设置
	NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream7_IRQn;  
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;  //抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
	NVIC_Init(&NVIC_InitStructure);  
	
	//串口发送DMA配置
	DMA_DeInit(DMA2_Stream7);  
	DMA_InitStructure.DMA_Channel = DMA_Channel_4;   
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART1->DR); //外设地址 
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Tx_Buf_Uart1;  	//内存地址
	DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;  			//dma传输方向
	DMA_InitStructure.DMA_BufferSize = TX_LEN;  						//设置传输缓冲区的长度  
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  	//设置DMA的外设递增模式 一个外设  
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  			//设置DMA的内存递增模式
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;  //外设数据字长 字节
	DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; //内存数据字长  
 
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; 						//设置DMA传输模式  
	//设置DMA优先级 
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;  
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;      
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;          
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;       	//内存、外设单次突发传输     
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;   

	//配置DMA2 Stream7的通道           
	DMA_Init(DMA2_Stream7, &DMA_InitStructure);    
	//DMA2 Stream7中断使能
	DMA_ITConfig(DMA2_Stream7,DMA_IT_TC,ENABLE);     


	//串口接收DMA配置
	DMA_DeInit(DMA2_Stream5);  
	DMA_InitStructure.DMA_Channel = DMA_Channel_4;   
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART1->DR);  
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Rx_Buf_Uart1;   
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;   
	DMA_InitStructure.DMA_BufferSize = RX_LEN;   
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;   
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;   
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;  
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;    
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;    
	DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;  
  
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;       
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;           
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;           
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;   
		
	//配置DMA2 收通道          
	DMA_Init(DMA2_Stream5, &DMA_InitStructure);    
	//使能通道
	DMA_Cmd(DMA2_Stream5,ENABLE);  
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);		 //使能GPIOA时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);		 //使能USART1时钟
 
	//串口1对应引脚复用映射
	GPIO_PinAFConfig(GPIOA,UART1_TX_PIN,GPIO_AF_USART1); 	//GPIOA9复用为USART1
	GPIO_PinAFConfig(GPIOA,UART1_RX_PIN,GPIO_AF_USART1); 	//GPIOA10复用为USART1
	
	//USART1端口配置
	GPIO_InitStructure.GPIO_Pin = UART1_TX_PIN | UART1_RX_PIN ; 	//GPIOA9与GPIOA10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;				//复用功能
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;			//速度50MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 				//推挽复用输出
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 				//上拉
	GPIO_Init(GPIOA,&GPIO_InitStructure); 						//初始化PA9，PA10

	//USART1 初始化设置
	USART_InitStructure.USART_BaudRate = Bound;										//波特率设置
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;						//字长为8位数据格式
	USART_InitStructure.USART_StopBits = USART_StopBits_1;							//一个停止位
	USART_InitStructure.USART_Parity = USART_Parity_No;								//无奇偶校验位
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;	//无硬件数据流控制
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;					//收发模式
	USART_Init(USART1, &USART_InitStructure); 										//初始化串口1
	

	//Usart1 NVIC 配置
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;			//串口1中断通道
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;		//抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =2;			//子优先级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;				//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);								
	
	//采用DMA方式发送串口数据  
	USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);  
	//采用DMA方式接收串口数据
	USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);

	//使能中断
	USART_ITConfig(USART1,USART_IT_TC,DISABLE);  
	USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);  
	USART_ITConfig(USART1,USART_IT_TXE,DISABLE);  
	USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);  
	
	//启动串口
	USART_Cmd(USART1, ENABLE);					
}

/**
 * @Description 初始化串口3 与蓝牙模块通信
 */
void Uart3_to_BT_Init(u32 Bound)
{	

	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;	
	DMA_InitTypeDef DMA_InitStructure;   
  
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);  
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);  
  
	//发送dma中断
	NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream3_IRQn;  
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;  
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
	NVIC_Init(&NVIC_InitStructure);  
	
	//配置发送DMA
	DMA_DeInit(DMA1_Stream3);  
	DMA_InitStructure.DMA_Channel = DMA_Channel_4;   
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART3->DR);  
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Tx_Buf_Uart3;  
	DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;  
	DMA_InitStructure.DMA_BufferSize = TX_LEN;  
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;  
	DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;  
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;  
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;  	
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;      
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;          
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;         
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;   
     
	DMA_Init(DMA1_Stream3, &DMA_InitStructure);    
	//串口发送DMA 传输完成中断
	DMA_ITConfig(DMA1_Stream3,DMA_IT_TC,ENABLE);     

	//配置串口接收DMA
	DMA_DeInit(DMA1_Stream1);  
	DMA_InitStructure.DMA_Channel = DMA_Channel_4;   
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART3->DR);  
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Rx_Buf_Uart3;   
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;   
	DMA_InitStructure.DMA_BufferSize = RX_LEN;   
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;   
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;   
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;  
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;    
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;    
	DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;  
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;       
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;           
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;           
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;   
		
	DMA_Init(DMA1_Stream1, &DMA_InitStructure);    

	//使能
	DMA_Cmd(DMA1_Stream1,ENABLE);  
	
	//配置IO、串口3
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); 
	RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
 
	//IO复用
	GPIO_PinAFConfig(GPIOB,UART3_TX_PIN,GPIO_AF_USART3); 
	GPIO_PinAFConfig(GPIOB,UART3_RX_PIN,GPIO_AF_USART3); 
	
	//配置IO
	GPIO_InitStructure.GPIO_Pin = UART3_TX_PIN | UART3_RX_PIN; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
	GPIO_Init(GPIOB,&GPIO_InitStructure); 

	//配置串口参数
	USART_InitStructure.USART_BaudRate = Bound;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	
	USART_Init(USART3, &USART_InitStructure); 
	

	//配置NVIC的串口中断
	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;			
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;	
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;		
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;				
	NVIC_Init(&NVIC_InitStructure);	
	
	//使能串口dma传输
	USART_DMACmd(USART3,USART_DMAReq_Tx,ENABLE);  
	USART_DMACmd(USART3,USART_DMAReq_Rx,ENABLE);

	//使能串口中断
	USART_ITConfig(USART3,USART_IT_TC,DISABLE);  
	USART_ITConfig(USART3,USART_IT_RXNE,DISABLE);  
	USART_ITConfig(USART3,USART_IT_TXE,DISABLE);  
	USART_ITConfig(USART3,USART_IT_IDLE,ENABLE);  
	
	//使能串口
	USART_Cmd(USART3, ENABLE);
	
}


/* -----------------------串口发送函数 --------------------------*/


/**
 * @Description 串口5发送数据
 */  
void Uart5_tx(uint8_t *data,uint16_t size)  
{  
    while (Flag_Tx_Uart5_Busy);  
    Flag_Tx_Uart5_Busy = 1;   
    memcpy(Tx_Buf_Uart5,data,size);  
    DMA_SetCurrDataCounter(DMA1_Stream7,size);   
    DMA_Cmd(DMA1_Stream7,ENABLE);  
} 


/**
 * @Description 发送0x55、超声波模块返回距离数据；发送”1“停止模块
 */
void ReceiveUltraData()
{
	static u8 senddata[1] = {0x55};
	Uart5_tx(senddata,1);
}
void SendStopflag()
{
	//发送ASCII码字符 “1”
	USART_SendData(USART6,0X31);
}


/**
 * @Description 串口1发送数据
 */  
void Uart1_to_PC_tx(uint8_t *data,uint16_t size)  
{  
    while (Flag_Tx_Uart1_Busy);  
    Flag_Tx_Uart1_Busy = 1;  
    memcpy(Tx_Buf_Uart1,data,size);  
    DMA_SetCurrDataCounter(DMA2_Stream7,size);  
    DMA_Cmd(DMA2_Stream7,ENABLE);  	//启动dma发送串口数据
}  


/**
 * @Description 串口3发送数据
 */  
void Uart3_to_BT_tx(uint8_t *data,uint16_t size)  
{   
    while (Flag_Tx_Uart3_Busy);  
    Flag_Tx_Uart3_Busy = 1;  
    memcpy(Tx_Buf_Uart3,data,size);   
	//设置DMA传输字节数、启动dma传输
    DMA_SetCurrDataCounter(DMA1_Stream3,size);   
    DMA_Cmd(DMA1_Stream3,ENABLE);  
} 

/**
 * @Description 串口5 发送dma中断 用户数据通过dma到达串口
 */
void DMA1_Stream7_IRQHandler(void)  
{  
	portENTER_CRITICAL();
	if(DMA_GetITStatus(DMA1_Stream7,DMA_IT_TCIF7) != RESET)   
    {  
        DMA_ClearFlag(DMA1_Stream7,DMA_FLAG_TCIF7);  
        DMA_Cmd(DMA1_Stream7,DISABLE);    
        USART_ITConfig(UART5,USART_IT_TC,ENABLE);  
    } 
	portEXIT_CRITICAL();	
}

/**
 * @Description 串口1 发送dma中断 用户数据通过dma到达串口
 */
void DMA2_Stream7_IRQHandler(void)   
{  
		portENTER_CRITICAL();
		if(DMA_GetITStatus(DMA2_Stream7,DMA_IT_TCIF7) != RESET)   
		{  
			//清除标志位  
			DMA_ClearFlag(DMA2_Stream7,DMA_FLAG_TCIF7);  
			//关闭发送DMA
			DMA_Cmd(DMA2_Stream7,DISABLE);  
			//开启串口1 发送完成中断	
			USART_ITConfig(USART1,USART_IT_TC,ENABLE);  
		}  
		portEXIT_CRITICAL();
}


/**
 * @Description 串口3 发送dma中断 用户数据通过dma到达串口
 */
void DMA1_Stream3_IRQHandler(void)   
{  
		portENTER_CRITICAL();
		if(DMA_GetITStatus(DMA1_Stream3,DMA_IT_TCIF3) != RESET)   
		{  
			//清除标志位  
			DMA_ClearFlag(DMA1_Stream3,DMA_FLAG_TCIF3);  
			//关闭DMA
			DMA_Cmd(DMA1_Stream3,DISABLE);  
			//开启串口3 发送完成中断	
			USART_ITConfig(USART3,USART_IT_TC,ENABLE);  
		}  
		portEXIT_CRITICAL();
}

/* ---------------------------串口接收代码-------------------------- */


/**
 * @Description 串口5 读取接收的数据到buf
 */
uint8_t Uart5_recive_data(uint8_t *buf)  
{  
   uint16_t len = 0;  
      
    if(USART_GetITStatus(UART5, USART_IT_IDLE) != RESET)  
    {  
        UART5->SR;  
        UART5->DR;
        DMA_Cmd(DMA1_Stream0,DISABLE);  
        DMA_ClearFlag(DMA1_Stream0,DMA_FLAG_TCIF0);  
        len = RX_LEN - DMA_GetCurrDataCounter(DMA1_Stream0);  
        memcpy(buf,Rx_Buf_Uart5,len);   
        DMA_SetCurrDataCounter(DMA1_Stream0,RX_LEN);  
        DMA_Cmd(DMA1_Stream0,ENABLE);  
        return len;  
    }   
	//未接收到数据
    return 0;  
}

/**
 * @Description 串口1 读取接收的数据到buf
 */   
uint8_t Uart1_recive_data(uint8_t *buf)  
{     
	uint16_t len = 0; 
	//串口空闲中断挂起，说明接收完成
	if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) 
	{	
		USART1->SR;  
		USART1->DR;
		//接收dma传输完成，关闭
		DMA_Cmd(DMA2_Stream5,DISABLE);
		DMA_ClearFlag(DMA2_Stream5,DMA_FLAG_TCIF5); 
		//接收数据长度=buf长度-dma剩余数据长度
		len = RX_LEN - DMA_GetCurrDataCounter(DMA2_Stream5);  
		//从串口缓存中复制数据
		memcpy(buf,Rx_Buf_Uart1,len);
			
		DMA_SetCurrDataCounter(DMA2_Stream5,RX_LEN);  
		//开启DMA传输。下一次接收
		DMA_Cmd(DMA2_Stream5,ENABLE);  

		return len;  
	}   		
	//未接收到数据
	return 0;  
} 
/**
 * @Description 串口3 读取接收的数据到buf
 */   
uint8_t Uart3_recive_data(uint8_t *buf)  
{     
	uint16_t len = 0; 
	//串口空闲中断挂起，说明接收完成
	if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) 
	{	
		USART3->SR;  
		USART3->DR;
		//关闭接收dma传输
		DMA_Cmd(DMA1_Stream1,DISABLE);
		DMA_ClearFlag(DMA1_Stream1,DMA_FLAG_TCIF1); 
		//接收数据长度=buf长度-dma剩余数据长度
		len = RX_LEN - DMA_GetCurrDataCounter(DMA1_Stream1); 
		//拷贝到应用程序
		memcpy(buf,Rx_Buf_Uart3,len);
			
		DMA_SetCurrDataCounter(DMA1_Stream1,RX_LEN);  
		//开启DMA传输。下一次接收
		DMA_Cmd(DMA1_Stream1,ENABLE);  

		return len;  
	}   		
	//未接收到数据
	return 0;  
} 

_Data_Rx Ultradata;
/**
* @Description 串口5 中断服务函数 因串口发送完成或串口接收完成而进入
 */
void UART5_IRQHandler(void)   
{  
	portENTER_CRITICAL();
    if(USART_GetITStatus(UART5, USART_IT_TC) != RESET)  
    {  
        USART_ITConfig(UART5,USART_IT_TC,DISABLE);  
        Flag_Tx_Uart5_Busy = 0;  
    }
	Ultradata.len = Uart5_recive_data(Ultradata.buf);  
	if (Ultradata.len != 0)  
	{ 
		//设置飞行器高度
		RT_Info.US100_Alt = ((((uint16_t)Ultradata.buf[0])<<8) + Ultradata.buf[1]);
	}
	portEXIT_CRITICAL();
} 


_Data_Rx PC_rx;
/**
* @Description 串口1 中断服务函数 因串口发送完成或串口接收完成而进入
 */
void USART1_IRQHandler(void)   
{  
	
	portENTER_CRITICAL();
	
	//判断是否发送完成
	if(USART_GetITStatus(USART1, USART_IT_TC) != RESET)  
    {  
		//关闭串口发送完成中断
        USART_ITConfig(USART1,USART_IT_TC,DISABLE);  
		//更新标志位，可以继续发送
        Flag_Tx_Uart1_Busy = 0;      
    }        

	PC_rx.len = Uart1_recive_data(PC_rx.buf);  
	if (PC_rx.len != 0)  
	{ 
		BaseType_t err;
		//同步数据处理任务
		xSemaphoreGiveFromISR(Data_Deal,&err);
	}
	portEXIT_CRITICAL();
} 


_Data_Rx Bluetooth_rx;
/**
* @Description 串口3 中断服务函数，因串口发送完成或串口接收完成而进入
 */
void USART3_IRQHandler(void)   
{  

	portENTER_CRITICAL();
	
	//判断是否发送完成
	if(USART_GetITStatus(USART3, USART_IT_TC) != RESET)  
    {  
		//关闭串口发送完成中断
        USART_ITConfig(USART3,USART_IT_TC,DISABLE);  
		//更新标志位，可以继续发送
        Flag_Tx_Uart3_Busy = 0;      
    }        
	Bluetooth_rx.len = Uart3_recive_data(Bluetooth_rx.buf);  
	
	if (Bluetooth_rx.len != 0)  
	{ 
		BaseType_t err;
		//同步数据处理任务
		xSemaphoreGiveFromISR(Data_Deal,&err);
	}
	portEXIT_CRITICAL();
} 